/*home page jquery*/
var modelArr = [ $(".add-subject"), $(".edit-subject"), $(".add-news"),
		$(".edit-news"), $(".news"), $(".news-show") ];

/* 显示新闻主页面 */
function showNews(path) {
	if (path != "") {
		window.location.href = path;
	} else {
		clearOther();
		$("#page-title").text("新闻中心");
		modelArr[4].css("display", "block");
	}
}

/* 打开添加主题页面 */
function addTheme() {
	clearOther();
	$("#page-title").text("添加主题");
	modelArr[0].css("display", "block");
}
/* 合法性验证 */
function checkNewTheme(x, id) {
	var theme = $("#news-themes>a");
	switch (x) {
	case 1:
		$("#add-theme-submit").attr("disabled", false);
		$("#add-theme-msg").text("");
		for (var i = 0; i < theme.length; i++) {
			if ($(theme[i]).text() == $.trim($("#add-theme-input").val())) {
				$("#add-theme-msg").text("主题已存在！");
				$("#add-theme-submit").attr("disabled", true);
			}
		}
		break;
	case 2:
		$("." + id + " input:nth-of-type(2)").attr("disabled", false);
		$("." + id + " span").text("");
		if ($("." + id + " input:nth-of-type(1)").val() == "11") {
			$("." + id + " span").text("当前新闻标题已存在！");
			$("." + id + " input:nth-of-type(2)").attr("disabled", true);
		}
		break;
	}

}

/* 打开编辑主题页面 */
function editTheme() {
	clearOther();
	$("#page-title").text("编辑主题");
	modelArr[1].css("display", "block");
}
/* 更新主题 */
function updateTheme(id, themeId, path) {
	if ($("#" + id + ">a:nth-of-type(1)").text() == '修改') {
		$("#" + id + ">input").attr("disabled", false);
		$("#" + id + ">input").css({
			"border" : "1px solid #eee",
			"background-color" : "#fff"
		});
		$("#" + id + ">a:nth-of-type(1)").text("保存");
	} else {
		$("#" + id + ">input").attr("disabled", true);
		$("#" + id + ">input").css({
			"border" : "none",
			"background-color" : "none"
		});
		$("#" + id + ">a:nth-of-type(1)").text("修改");
		// 提交修改
		$.ajax({
			type : 'POST',
			url : path + "updateTheme",
			data : {
				'themeId' : themeId,
				'themeName' : $("#" + id + ">input").val()
			},
			success : function() {
				window.location.href = path;
			}
		});
	}
}
/* 删除主题 */
function deleteTheme(themeId, path) {
	if (confirm("确认删除！")) {
		$.ajax({
			type : 'POST',
			url : path + "deleteTheme",
			data : {
				'themeId' : themeId
			},
			success : function() {
				window.location.href = path;
			}
		});
	}
}

/* 打开添加新闻页面 */
function addNews() {
	clearOther();
	$("#page-title").text("添加新闻");
	modelArr[2].css("display", "block");
}

/* 打开编辑新闻页面 */
function editNews() {
	clearOther();
	$("#page-title").text("编辑新闻");
	modelArr[3].css("display", "block");
}
/* 更新新闻 */
function updateNews(id, newsId, path) {
	if ($("#" + id + ">a:nth-of-type(1)").text() == '修改') {
		$("#" + id + ">input").attr("disabled", false);
		$("#" + id + ">input").css({
			"border" : "1px solid #eee",
			"background-color" : "#fff"
		});
		$("#" + id + "+textarea").css("display", "block");
		$("#" + id + ">a:nth-of-type(1)").text("保存");
	} else {
		$("#" + id + ">input").attr("disabled", true);
		$("#" + id + ">input").css({
			"border" : "none",
			"background-color" : "none"
		});
		$("#" + id + "+textarea").css("display", "none");
		$("#" + id + ">a:nth-of-type(1)").text("修改");
		// 提交修改
		$.ajax({
			type : 'POST',
			url : path + "updateNews",
			data : {
				'newsId' : newsId,
				'newsTitle' : $("#" + id + ">input").val(),
				'newsContent' : $("#" + id + "+textarea").val()
			},
			success : function() {
				window.location.href = path;
			}
		});
	}
}
/* 删除新闻 */
function deleteNews(newsId, path) {
	if (confirm("确认删除！")) {
		$.ajax({
			type : 'POST',
			url : path + "deleteNews",
			data : {
				'newsId' : newsId
			},
			success : function() {
				window.location.href = path;
			}
		});
	}
}

/* 清理页面 */
function clearOther() {
	for ( var i in modelArr) {
		modelArr[i].css("display", "none");
	}
}

/* 显示具体新闻页 */
function openNews(id) {
	clearOther();
	$("#page-title").text("新闻详细");
	$("#news-detailed-" + id).css("display", "block");
}

/* 显示左侧列表国内外具体新闻页 */
function openNewsI(id) {
	clearOther();
	$("#page-title").text("新闻详细");
	$("#news-internal-detailed-" + id).css("display", "block");
}
function openNewsE(id) {
	clearOther();
	$("#page-title").text("新闻详细");
	$("#news-external-detailed-" + id).css("display", "block");
}
